﻿using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BCCommon
{
    public static class GeneralContainer
    {
        private static IConfiguration configuration;
        private static IServiceProvider serviceProvider;

        public static IConfiguration Configuration
        {
            get
            {
                if (configuration == null)
                {
                    throw new NullReferenceException();
                }
                return configuration;
            }
            set
            {
                if (configuration == null)
                {
                    if (value.GetType().GetTypeInfo().ImplementedInterfaces.Contains(typeof(IConfiguration)))
                    {
                        configuration = value;
                    }
                    else
                    {
                        throw new NotImplementedException("当前对象并不是此接口的实现！");
                    }
                }
            }
        }
        /// <summary>
        /// 全局服务提供接口对象
        /// </summary>
        public static IServiceProvider ServiceProvider
        {
            get
            {
                if (serviceProvider == null)
                {
                    throw new NullReferenceException();
                }
                return serviceProvider;
            }
            set
            {
                if (serviceProvider == null)
                {
                    if (value.GetType().GetTypeInfo().ImplementedInterfaces.Contains(typeof(IServiceProvider)))
                    {
                        serviceProvider = value;
                    }
                    else
                    {
                        throw new NotImplementedException("当前对象并不是此接口的实现！");
                    }
                }
            }
        }

        /// <summary>
        /// Json序列化设置
        /// </summary>
        public static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
        {
            ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy { ProcessDictionaryKeys = false } },
            Converters = new List<JsonConverter> {
                new StringEnumConverter { AllowIntegerValues = true },
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeLocal, DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }
            },
            NullValueHandling = NullValueHandling.Ignore
        };

        /// <summary>
        /// Android 自定义通知栏样式，取值：1-100
        /// </summary>
        public const int ANDROID_NOTIFICATION_BAR_TYPE = 66;
    }
}
